﻿using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Forms;
using BestCode.Creds.Entities;
using BestCode.Creds.UI.Common;
using BestCode.Creds.UI.ViewModel;
using UserControl = System.Windows.Controls.UserControl;

namespace BestCode.Creds.UI
{
    /// <summary>
    /// Interaction logic for VwEntities.xaml
    /// </summary>
    public partial class VwEntities : UserControl
    {
       
        private readonly VmEntity viewModel;
        public event EventHandler OnEntitySelected;

        private Window modalWindow;
        private VwEntitiesChildren modalControl;

        public VwEntities()
        {
            InitializeComponent();
            if (!(bool)(DesignerProperties.IsInDesignModeProperty.GetMetadata(typeof(DependencyObject)).DefaultValue))
            {
                viewModel = new VmEntity();
                this.DataContext = viewModel;
            }
        }

        private void btnPersons_OnClick(object sender, RoutedEventArgs e)
        {
            if (OnEntitySelected != null)
            {
                var obj = ((FrameworkElement)sender).DataContext as EntityInfo;
                OnEntitySelected(obj, EventArgs.Empty);
            }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            System.Windows.Forms.FolderBrowserDialog fbd = new System.Windows.Forms.FolderBrowserDialog();
            fbd.SelectedPath = viewModel.SelectedItem.PicturesFolder;
            var result = fbd.ShowDialog();

            if (result.ToString().Equals("OK"))
               viewModel.SelectedItem.PicturesFolder = fbd.SelectedPath;
        }

        private void BtnChildrenEntities_OnClick(object sender, RoutedEventArgs e)
        {
            var obj = ((FrameworkElement)sender).DataContext as EntityInfo;
            ShowModalWindow(obj);
        }

        private void ShowModalWindow(EntityInfo entity)
        {
            modalControl = new VwEntitiesChildren(entity);
            modalControl.OnCloseControl += modalControl_OnCloseControl;
            modalWindow = new Window
            {
                Title = "New",
                Content = modalControl,
                Height = 550,
                Width = 600,
                AllowsTransparency = true,
                WindowStartupLocation = WindowStartupLocation.CenterOwner,
                WindowStyle = WindowStyle.None,

                //SizeToContent = SizeToContent.WidthAndHeight,
                ResizeMode = ResizeMode.NoResize
            };

            modalWindow.Owner = WindowUtil.GetParentWindow(this);
            WindowUtil.ApplyEffect(WindowUtil.GetParentWindow(this));
            modalWindow.ShowDialog();
            WindowUtil.ClearEffect(WindowUtil.GetParentWindow(this));
        }

        void modalControl_OnCloseControl(object sender, EventArgs e)
        {
            modalWindow.Close();
        }


    }
}
